package edu.uta.shorthorn.news.ui;

import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import edu.uta.shorthorn.news.constants.*;
import edu.uta.shorthorn.news.model.*;
import edu.uta.shorthorn.news.parser.*;

public class YouTube extends Activity {
	/** Called when the activity is first created. */
	ListView headlines;
	ArrayList<String> array = new ArrayList<String>(); 
	LinearLayout layout;
	private List<Message> messages;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.youtube_content);
		
		try
		{
			FeedParser parser = FeedParserFactory.getParser(ParserType.ANDROID_SAX, YouTubeFeedLinks.feedLink);
			messages = parser.parse();	  

			headlines = (ListView) findViewById(R.id.headlines);
			ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.rowlayout, R.id.listItem, array);
			
			for (Message msg : messages)
			{
				adapter.add(msg.getTitle());
			}
			headlines.setAdapter(adapter);	 
		} 
		catch (Throwable t)
		{
			Log.e("ShorthornNews",t.getMessage(),t);
		}
		headlines.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) { 
				Intent intent = new Intent().setClass(YouTube.this, YouTubeContent.class);
				String title = messages.get(position).getTitle();
				String pubDate = messages.get(position).getDate();
				String author = messages.get(position).getAuthor(); 
				String link = messages.get(position).getLink().toString();

				intent.putExtra("title", title);
				intent.putExtra("pubDate", pubDate);
				intent.putExtra("author", author);
				intent.putExtra("link", link);
				startActivity(intent);
			}
		});
	}
}


